package com.cx.io;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author zhangshuai
 * @version V1.0
 * @Project Java2_26
 * @Package com.cx.io
 * @Description:
 * @Date 2025/5/23 17:02
 */

public class FileOutputReaderTest {
    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("ccc.tex"));
        bufferedWriter.write("## JDK1.8 新特性\n" +
                "\n" +
                "- 是java语言开发的一个很重要的版本，Oracle 公司于 2014 年3月18日发布的。\n" +
                "- 支持\n" +
                "  - Lambda 表达式\n" +
                "  - 函数式接口 ***\n" +
                "  - 新的Stream API ***\n" +
                "  - 新的日期时间 API\n" +
                "\n" +
                "### 1. Lambda表达式\n" +
                "\n" +
                "- 特殊的匿名内部类，语法更加的简洁\n" +
                "\n" +
                "- 基本语法：\n" +
                "\n" +
                "  - 函数式接口  变量名 = (参数列表)->{}\n" +
                "    - Runnable\n" +
                "  - Compartor\n" +
                "\n" +
                "- 注意问题\n" +
                "\n" +
                "  - 形参列表的数据类型可以自动推断。\n" +
                "  - 形参为空，保留小括号。\n" +
                "  - 形参只有一个，可以省略小括号。\n" +
                "  - 如果执行语句只有一句，且没有返回值，可以省略大括号。\n" +
                "  - 如果执行语句只有一句，且有返回值，如果省略大括号，那么必须return 语句一起省略。\n" +
                "  - Lambda 不会生成一个单独的内部类文件。\n" +
                "\n" +
                "- ```java\n" +
                "  package com.qf.lambda;\n" +
                "  \n" +
                "  import java.util.Comparator;\n" +
                "  import java.util.TreeSet;\n" +
                "  \n" +
                "  /**\n" +
                "   * Lambda 表达式 1：是一些特殊的接口的匿名子类对象的特殊的表达方式。\n" +
                "   * \t\t特殊的接口：接口只能有一个抽象的方法没有实现。\n" +
                "   * \t\t可以让代码变得很简洁。\n" +
                "   * \n" +
                "   * Lambda 的语法：\n" +
                "   * \t父接口类型 父接口引用 = (形参列表)->{方法体}\n" +
                "   * \t\n" +
                "   * 特殊的语法：\n" +
                "   * \t1：如果形参列表只有一个参数，那么小括号可以省略。形参的类型也可以省略。\n" +
                "   * \t2: 如果方法体只有一句代码，也可以省略大括号。\n" +
                "   * \t\t有返回值：如果省略大括号，return 也得省略。\n" +
                "   * \t\t无返回值：直接省略大括号即可。\n" +
                "   *\t3：形参列表为空，必须有小括号。\n" +
                "   * \n" +
                "   *\n" +
                "   */\n" +
                "  public class TestLambda {\n" +
                "  \n" +
                "  \tpublic static void main(String[] args) {\n" +
                "  \t\t// 匿名内部类。\n" +
                "  \t\tRunnable runnable = new Runnable() {\n" +
                "  \t\t\t@Override\n" +
                "  \t\t\tpublic void run() {\n" +
                "  \t\t\t\tSystem.out.println(\"run\");\n" +
                "  \t\t\t}\n" +
                "  \t\t};\n" +
                "  \t\tnew Thread(runnable).start();\n" +
                "  \n" +
                "  \t\t// Lambda\n" +
                "  \t\tRunnable run = () -> {\n" +
                "  \t\t\tSystem.out.println(\"run1\");\n" +
                "  \t\t};\n" +
                "  \t\tnew Thread(run).start();\n" +
                "  \t\t\n" +
                "  \t\tnew Thread(() -> {\n" +
                "  \t\t\tSystem.out.println(\"run2\");\n" +
                "  \t\t}).start();\n" +
                "  \t\t\n" +
                "  //\t\t形参列表只有一个的情况\n" +
                "  \t\tComparable<Integer> com = o -> 1;//有返回值的写法。\n" +
                "  \t\tMyInterface interface1 = o->System.out.println(o);//无返回值的写法。\n" +
                "  \t\t\n" +
                "  \t\t\n" +
                "  \t\tComparator<Integer> com1 = new Comparator<Integer>() {\n" +
                "  \t\t\tpublic int compare(Integer o1, Integer o2) {\n" +
                "  \t\t\t\treturn o1-o2;\n" +
                "  \t\t\t};\n" +
                "  \t\t};\n" +
                "  \t\tTreeSet<Integer> set = new TreeSet<>(com1);\n" +
                "  \t\t\n" +
                "  \t\tComparator<Integer> com2 = (o1,o2)->o1-o2;\n" +
                "  \t\t\n" +
                "  \t\tTreeSet<Integer> set1 = new TreeSet<>(com2);\n" +
                "  \t\t\n" +
                "  \t\tTreeSet<Integer> set2 = new TreeSet<>((o1,o2)->o1-o2);\n" +
                "  \t\t\t\n" +
                "  \t}\n" +
                "  }\n" +
                "  \n" +
                "  interface MyInterface{\n" +
                "  \tvoid test(Object o);\n" +
                "  }\n" +
                "  ```\n" +
                "\n" +
                "- \n" +
                "\n" +
                "### 2. 函数式接口 ***\n" +
                "\n" +
                "- **有且仅有一个抽象方法的接口**，称为函数式接口。只有函数式接口才能使用 Lambda 表达式。\n" +
                "\n" +
                "- JDK提供了大量常用的函数式接口以丰富Lambda的典型使用场景，它们主要在 java.util.function 包中被提供。\n" +
                "\n" +
                "- @FunctionalInterface 注解用于检测接口是否符合函数式接口\n" +
                "\n" +
                "- 常见的函数式接口\n" +
                "\n" +
                "  - **Supplier 生产型接口**\n" +
                "    - java.util.function.Supplier 接口仅包含一个无参的方法： T get() 。用来获取一个泛型参数指定类型的对象数据。Supplier接口被称之为生产型接口,指定接口的泛型是什么类型,那么接口中的get方法就会生产什么类型的数据。\n" +
                "    - **无参，有返回**\n" +
                "  - **Consumer 消费型接口**\n" +
                "    - java.util.function.Consumer 接口则正好与Supplier接口相反，它不是生产一个数据，而是**消费**一个数据（至于具体怎么消费(使用), 需要自定义(输出，计算…） 其数据类型由泛型决定。Consumer 接口中包含抽象方法 void accept(T t) ，意为消费一个指定泛型的数据。\n" +
                "    - **有参，无返回**\n" +
                "  - **Predicate 断言接口**\n" +
                "    - 有时候我们需要对某种类型的数据进行判断，从而得到一个boolean值结果。这时可以使用 java.util.function.Predicate 接口。Predicate 接口中包含一个抽象方法： boolean test(T t) 。\n" +
                "    - **有参，返回boolean**\n" +
                "  - **Function 函数型接口**\n" +
                "    - java.util.function.Function<T,R> 接口用来根据一个类型的数据得到另一个类型的数据，前者称为前置条件，后者称为后置条件。Function 接口中最主要的抽象方法为： R apply(T t) ，根据类型T的参数获取类型R的结果。\n" +
                "    - **有参，有返回**\n" +
                "\n" +
                "- 案例：\n" +
                "\n" +
                "  - Supplier 生产随机的钱，Consumer 负责不同钱的消费。\n" +
                "\n" +
                "  - Predicate 人名过滤功能。\n" +
                "\n" +
                "  - Function 大小写的转换。\n" +
                "\n" +
                "  - ```java\n" +
                "    package com.qf.lambda;\n" +
                "    \n" +
                "    import java.util.ArrayList;\n" +
                "    import java.util.Arrays;\n" +
                "    import java.util.List;\n" +
                "    import java.util.function.Consumer;\n" +
                "    import java.util.function.Function;\n" +
                "    import java.util.function.Predicate;\n" +
                "    import java.util.function.Supplier;\n" +
                "    \n" +
                "    /**\n" +
                "     * 函数式接口：\n" +
                "     * \t特点：如果一个接口中只有一个抽象方法，那么该接口就称为函数式接口。\n" +
                "     * \t\t只有函数式接口才能使用 Lambda 表达式。\n" +
                "     * \n" +
                "     * JDK 提供的函数式接口： java.util.function 都在这个包下。\n" +
                "     * \t@FunctionalInterface 注解用来检查 接口是否满足 函数式接口。\n" +
                "     * \t1：供给型接口：Supplier \n" +
                "     * \t\t方法：无参有返回。\n" +
                "     * \t\t T get(); \n" +
                "     * \t\t通过该接口可以获取 T 类型的数据。\n" +
                "     * \t2：消费型接口：Consumer \n" +
                "     * \t\t方法：有参无返回。\n" +
                "     * \t\tvoid accept(T t);\n" +
                "     * \t\t可以消费 T 类型的数据。\n" +
                "     * \t3：断言型接口：Predicate \n" +
                "     * \t\t方法：内容过滤，有参，有返回boolean。\n" +
                "     * \t\tboolean test(T t);\n" +
                "     * \t\t判断 T 类型的 数据是否满足你的的要求。\n" +
                "     * \t4：函数型接口：Function \n" +
                "     * \t\t方法：做某些功能的实现，有参，有返回。\n" +
                "     * \t\t R apply(T t);\n" +
                "     * \t\t对 T 类型的数据，进行加工，返回 R 类型的数据。\n" +
                "     */\n" +
                "    public class TestFunctionInterface {\n" +
                "    \n" +
                "    \tpublic static void main(String[] args) {\n" +
                "    //\t\t1：供给型接口：Supplier \n" +
                "    \t\tint[] array = getArray(new Supplier<Integer>() {\n" +
                "    \t\t\tpublic Integer get() {\n" +
                "    \t\t\t\treturn (int)(Math.random() * 100);\n" +
                "    \t\t\t}\n" +
                "    \t\t}, 10);\n" +
                "    \t\t\n" +
                "    \t\tSystem.out.println(Arrays.toString(array));\n" +
                "    //\t\tLambda\n" +
                "    \t\tarray = getArray(()->(int)(Math.random() * 100), 10);\n" +
                "    \t\tSystem.out.println(Arrays.toString(array));\n" +
                "    \t\t\n" +
                "    //\t\t2：消费型接口：Consumer \n" +
                "    \t\thappy(new Consumer<Integer>() {\n" +
                "    \t\t\t@Override\n" +
                "    \t\t\tpublic void accept(Integer t) {\n" +
                "    \t\t\t\tSystem.out.println(\"佳欣花费了：\"+ t +\"元去看了一段让自己上火的舞蹈！\");\n" +
                "    \t\t\t}\n" +
                "    \t\t}, 1000);\n" +
                "    \t\t\n" +
                "    \t\thappy(t->System.out.println(\"佳欣花费了：\"+ t +\"元去看了一段让自己上火的舞蹈！\"), 1000);\n" +
                "    \t\t\n" +
                "    \t\t\n" +
                "    //\t\t3：断言型接口：Predicate \n" +
                "    \t\tList<Integer> list = filterNums(new Predicate<Integer>() {\n" +
                "    \t\t\tpublic boolean test(Integer t) {\n" +
                "    \t\t\t\treturn t%2==0;\n" +
                "    \t\t\t}\n" +
                "    \t\t}, array);\n" +
                "    \t\tSystem.out.println(list);\n" +
                "    \t\t\n" +
                "    \t\tList<Integer> list1 = filterNums(t->t%2==0, array);\n" +
                "    \t\tSystem.out.println(list1);\n" +
                "    \t\t\n" +
                "    //\t\t函数型接口：Function \n" +
                "    \t\tString case1 = toUpperCase(new Function<String, String>() {\n" +
                "    \t\t\t@Override\n" +
                "    \t\t\tpublic String apply(String t) {\n" +
                "    \t\t\t\treturn t.toUpperCase();\n" +
                "    \t\t\t}\n" +
                "    \t\t}, \"abc\");\n" +
                "    \t\t\n" +
                "    \t\tSystem.out.println(case1);\n" +
                "    \t\t\n" +
                "    \t\tString case2 = toUpperCase(t->t.toUpperCase(), \"2134fjlerjoJLFJDLKdlkjfd\");\n" +
                "    \t\t\n" +
                "    \t\tSystem.out.println(case2);\n" +
                "    \t\t\n" +
                "    \t}\n" +
                "    \t\n" +
                "    //\t通过 Supplier 接口，获取若干个(count)整数，组成一个数组。\n" +
                "    \tpublic static int[] getArray(Supplier<Integer> supplier,int count){\n" +
                "    \t\tint[] arr = new int[count];\n" +
                "    \t\t\n" +
                "    \t\tfor (int i = 0; i < count; i++) {\n" +
                "    \t\t\tarr[i] = supplier.get();\n" +
                "    \t\t}\n" +
                "    \t\treturn arr;\n" +
                "    \t}\n" +
                "    \t\n" +
                "    //\t消费钱去happy\n" +
                "    \tstatic void happy(Consumer<Integer> consumer,int money){\n" +
                "    \t\tconsumer.accept(money);\n" +
                "    \t}\n" +
                "    \t\n" +
                "    //\t将一个数组中的内容的偶数部分以List 形式返回。\n" +
                "    \tstatic List<Integer> filterNums(Predicate<Integer> predicate,int[] arr){\n" +
                "    \t\tList<Integer> list = new ArrayList<Integer>();\n" +
                "    \t\t\n" +
                "    \t\tfor (int i = 0; i < arr.length; i++) {\n" +
                "    //\t\t\t元素满足要求，添加到List 中。\n" +
                "    \t\t\tif(predicate.test(arr[i])){\n" +
                "    \t\t\t\tlist.add(arr[i]);\n" +
                "    \t\t\t}\n" +
                "    \t\t}\n" +
                "    \t\treturn list;\n" +
                "    \t}\n" +
                "    \t\n" +
                "    //\t将任意的字符串转换为大写。\n" +
                "    \tstatic String toUpperCase(Function<String, String> fun,String str){\n" +
                "    \t\treturn fun.apply(str);\n" +
                "    \t}\n" +
                "    }\n" +
                "    ```\n" +
                "\n" +
                "  - \n" +
                "\n" +
                "### 3. 方法引用（了解）\n" +
                "\n" +
                "- 方法引用：Lambda表达式的简写形式。\n" +
                "- 如果Lambda表达式方法体中只是调用一个特定的已经存在的方法，则可以使用方法引用。\n" +
                "- 常用形式：\n" +
                "  - 对象::实例方法\n" +
                "  - 类::静态方法\n" +
                "  - 类::实例方法\n" +
                "  - 类::new\n" +
                "\n" +
                "```java\n" +
                "package com.qf.day28;\n" +
                "\n" +
                "import java.util.Comparator;\n" +
                "import java.util.function.Consumer;\n" +
                "import java.util.function.Function;\n" +
                "import java.util.function.Supplier;\n" +
                "\n" +
                "/**\n" +
                " * 方法引用：Lambda表达式的简写形式。\n" +
                " * 如果Lambda表达式方法体中只是调用一个特定的已经存在的方法，则可以使用方法引用。\n" +
                " */\n" +
                "public class MethodRef {\n" +
                "    public static void main(String[] args) {\n" +
                "        //形式1: 对象::实例方法\n" +
                "        Consumer<String> consumer1=s-> System.out.println(s);\n" +
                "        //（1）Lambda表达式只能包含一个方法调用 (2) 调用的方法的参数和返回值和接口中方法一致。\n" +
                "        Consumer<String> consumer2=System.out::println;\n" +
                "        consumer1.accept(\"hello\");\n" +
                "        consumer2.accept(\"world\");\n" +
                "        //形式2：类::静态方法\n" +
                "        //（1）Lambda表达式只能包含一个方法调用 (2)调用的方法的参数和返回值和接口中方法一致。\n" +
                "        Comparator<Integer> com1=(o1,o2)->Integer.compare(o1, o2);\n" +
                "        Comparator<Integer> com2=Integer::compare;\n" +
                "        //形式3：类::实例方法\n" +
                "        //（1）Lambda表达式只能包含一个方法调用 (2)一个参数作为调用者，其他作为参数\n" +
                "        Comparator<Integer> com3=(o1,o2)->o1.compareTo(o2);\n" +
                "        Comparator<Integer> com4=Integer::compareTo;\n" +
                "        //形式4：类::new\n" +
                "        //（1）Lambda表达式只能包含一个构造方法调用  (2)调用的构造方法的参数和接口中方法参数一致。\n" +
                "        Supplier<Student> sup1=()->new Student();\n" +
                "        Supplier<Student> sup2=Student::new;\n" +
                "        Student s1 = sup1.get();\n" +
                "        Student s2 = sup2.get();\n" +
                "\n" +
                "        MySupplier<Student> sup3=(name,age)->new Student(name,age);\n" +
                "        MySupplier<Student> sup4=Student::new;\n" +
                "\n" +
                "        Student s3 = sup3.get(\"张三\", 20);\n" +
                "        Student s4 = sup4.get(\"李四\", 22);\n" +
                "        //形式5:元素类型[]::new\n" +
                "        //（1）Lambda表达式只能包含一个数组的创建。（2）参数作为数组的长度\n" +
                "        Function<Integer,Student[]> fun1=n->new Student[n];\n" +
                "        Function<Integer,Student[]> fun2=Student[]::new;\n" +
                "        Student[] students = fun2.apply(5);\n" +
                "        System.out.println(students.length);\n" +
                "    }\n" +
                "}\n" +
                "\n" +
                "```\n" +
                "\n" +
                "### 4. Stream ***(Lambda应用)\n" +
                "\n" +
                "- Stream 与集合类似，但集合中保存的是数据，而Stream中保存的是对集合或者数组的数据的操作。所以 **Stream是对数据集操作的集合**\n" +
                "\n" +
                "- Stream 特点\n" +
                "\n" +
                "  - 不会存储元素。\n" +
                "  - 不会改变源对象，它只会返回一个持有结果的新的Stream。\n" +
                "  - 操作的是延迟执行的，等需要结果的时候才会执行。\n" +
                "\n" +
                "- **Stream 体系**\n" +
                "\n" +
                "  - BaseStream 接口\n" +
                "    - 四个子接口 DoubleStream、IntStream、LongStream、Stream\n" +
                "\n" +
                "- Stream的使用步骤\n" +
                "\n" +
                "  - 创建流：新建一个流。\n" +
                "  - 中间操作：在一个或者多个步骤，将初始的Stream转换到另外一个Stream。\n" +
                "  - 终止操作：使用一个终止操作来产生一个结果，该操作会强制之前的延迟操作立即执行，然后Stream就不能使用了。\n" +
                "\n" +
                "- Stream的创建方式：\n" +
                "\n" +
                "  - **使用Collection接口中stream()或parallelStream()。**\n" +
                "\n" +
                "  - **通过Arrays类的stream()方法。**\n" +
                "\n" +
                "  - 通过Stream接口的of()、iterate()、generate()方法。\n" +
                "\n" +
                "    - Stream.iterate：迭代流  limit() 限制次数\n" +
                "\n" +
                "  - 通过IntStream、LongStream、DoubleStream接口中的of、range、rangeClosed方法。\n" +
                "\n" +
                "    - 范围控制\n" +
                "\n" +
                "  - ```java\n" +
                "    package com.qf.stream;\n" +
                "    \n" +
                "    import java.util.Arrays;\n" +
                "    import java.util.List;\n" +
                "    import java.util.function.UnaryOperator;\n" +
                "    import java.util.stream.IntStream;\n" +
                "    import java.util.stream.Stream;\n" +
                "    \n" +
                "    import com.qf.util.MyUtil;\n" +
                "    \n" +
                "    /**\n" +
                "     * Stream: 流 1：Stream 流 和 IO 流没有任何关系。 IO 流 是信息的通道。通过通道流对象，可以实现对数据的读写操作。\n" +
                "     * 主要是针对容器中的元素的操作的功能的增强。 1：批量处理容器中的数据。 2：聚合操作（max、min、sum...avg count） 2: Stream\n" +
                "     * 支持了 并行流（多线程）处理 容器中的数据。\n" +
                "     * \n" +
                "     * 3：大量的使用函数式接口，使用 Lambda 表达式。\n" +
                "     * \n" +
                "     * 4：Stream 流 分为两类 1：中间操作。各种对数据的处理的操作。 一般会采用链式编程。 2：终结操作。将前面的所有的中间操作生效。\n" +
                "     * 执行终结操作，Stream就不可用了。 5：对流的操作分为了2类 1：有状态操作 具有存储功能的操作，后面的数据的处理要依赖前面处理的数据。\n" +
                "     * 2：无状态操作 不具有存储功能的操作，后面的数据的处理不依赖前面处理的数据。\n" +
                "     * \n" +
                "     * 6：流的使用流程： 1：获取流-->n次 中间操作-->终结操作\n" +
                "     * \n" +
                "     * 7：流的获取 \n" +
                "     * \t\t1：使用 Collection 接口中的 stream() 方法。(串行流)\n" +
                "     * \t\t2：使用 Collection 接口中的 parallelStream();并行流。底层使用了多线程。\n" +
                "     * \t\t\t优点：效率高。\n" +
                "     * \t\t\t缺点：数据的处理的顺序可能错乱。\n" +
                "     * \n" +
                "     * \t\t3: Arrays类的stream()方法\n" +
                "     * \t\t4: Stream接口的of()、iterate()、generate()方法。\n" +
                "     * \t\t5:通过IntStream、LongStream、DoubleStream接口中的of、range、rangeClosed方法。\n" +
                "     * \n" +
                "     */\n" +
                "    public class TestStream {\n" +
                "    \n" +
                "    \tpublic static void main(String[] args) {\n" +
                "    \t\t// 1：使用 Collection 接口中的 stream() 方法。\n" +
                "    \t\tList<String> list = Arrays.asList(\"hello\", \"break\", \"continue\", \"annotation\", \"synchronization\");\n" +
                "    \t\tStream<String> stream = list.stream();\n" +
                "    //\t\t终结遍历操作。\n" +
                "    \t\tlist.forEach(s->System.out.println(s));\n" +
                "    \t\t\n" +
                "    //\t\t2：使用 Collection 接口中的 parallelStream();并行流。底层使用了多线程。\n" +
                "    \t\tStream<String> stream2 = list.parallelStream();\n" +
                "    \t\tlist.forEach(s->System.out.println(s));\n" +
                "    \t\t\n" +
                "    \t\tObject[] array = list.toArray();\n" +
                "    //\t\tlist.toArray(a)\n" +
                "    //\t\t3：Arrays类的stream()方法\n" +
                "    \t\tStream<Object> stream3 = Arrays.stream(array);\n" +
                "    //\t\tArrays.stream(array, startInclusive, endExclusive)\n" +
                "    \t\tstream3.forEach(s->System.out.println(s));\n" +
                "    \t\t\n" +
                "    //\t\t4: Stream接口的of()、iterate()、generate()方法。\n" +
                "    \t\tStream<String> stream4 = Stream.of(\"a\",\"b\");\n" +
                "    //\t\tStream 处理的数据源来自于 供给接口。\n" +
                "    \t\tStream<Integer> stream5 = Stream.generate(()->MyUtil.random(0, 100));\n" +
                "    //\t\tstream5.limit(100).forEach(s->System.out.println(s));\n" +
                "    \t\tSystem.out.println(\"-------------\");\n" +
                "    \t\tStream<Integer> stream6 = Stream.iterate(1, t->t+1);\n" +
                "    \t\t\n" +
                "    \t\t//1 \n" +
                "    \t\tStream<Integer> stream7 = Stream.iterate(1, new UnaryOperator<Integer>(){\n" +
                "    \t\t\t@Override\n" +
                "    \t\t\tpublic Integer apply(Integer t) {\n" +
                "    \t\t\t\tSystem.out.println(t);\n" +
                "    \t\t\t\treturn t+1;\n" +
                "    \t\t\t}});\n" +
                "    \t\n" +
                "    \t\t\n" +
                "    \t\tstream7.limit(10).forEach(s->System.out.println(s));\n" +
                "    //\t\t\n" +
                "    \t\tstream6.limit(30).forEach(s->System.out.println(s));\n" +
                "    \t\t\n" +
                "    //\t\tIntStream\n" +
                "    \t\tIntStream range = IntStream.range(30, 60);\n" +
                "    \t\trange.forEach(s->System.out.println(s));\n" +
                "    \t\t\n" +
                "    \t}\n" +
                "    }\n" +
                "    ```\n" +
                "\n" +
                "  - \n" +
                "\n" +
                "- Stream 的中间操作\n" +
                "\n" +
                "  - filter、limit、skip、distinct、sort\n" +
                "\n" +
                "  - map\n" +
                "\n" +
                "  - parallel\n" +
                "\n" +
                "  - ```java\n" +
                "    package com.qf.stream;\n" +
                "    \n" +
                "    import java.util.ArrayList;\n" +
                "    import java.util.Arrays;\n" +
                "    import java.util.List;\n" +
                "    import java.util.stream.Stream;\n" +
                "    \n" +
                "    import com.qf.util.MyUtil;\n" +
                "    \n" +
                "    /**\n" +
                "     * 流的中间操作\n" +
                "     * \t1：中间操作之后，会产生新的Stream 对象。可以继续中间操作，或者是终结操作。\n" +
                "     * \t\t返回新的Stream 对象，对开始的Stream 对象没有影响。\n" +
                "    - filter、\n" +
                "    \tlimit、\n" +
                "    \tskip、\n" +
                "    \tdistinct、\n" +
                "    \tsort\n" +
                "    - map\n" +
                "    - parallel *\n" +
                "     */\n" +
                "    public class StreamMidTest1 {\n" +
                "    \n" +
                "    \tpublic static void main(String[] args) {\n" +
                "    \t\tList<String> list = Arrays.asList(\"int\",\"byte\",\"boolean\",\"float\",\"double\",\"char\",\"short\",\"long\");\n" +
                "    \t\t\n" +
                "    \t\tStream<String> stream = list.stream();\n" +
                "    \t\t\n" +
                "    //\t\t中间操作。\n" +
                "    //\t\tfilter 过滤功能\n" +
                "    \t\tstream.filter(s->s.length()<=5).forEach(s->System.out.println(s));\n" +
                "    \t\t\n" +
                "    \t\tSystem.out.println(\"-----------------\");\n" +
                "    //\t\t流一旦终结操作之后，就不能使用了。\n" +
                "    \t\tStream<String> stream1 = list.stream();\n" +
                "    //\t\tlimit 限制|极限  取数据集中的前n个。\n" +
                "    \t\tstream1.filter(s->s.length()<=5).limit(5).forEach(s->System.out.println(s));\n" +
                "    \t\tSystem.out.println(\"-----------------\");\n" +
                "    //\t\tskip 跳过前n 个数据。\n" +
                "    \t\tStream<String> stream2 = list.stream();\n" +
                "    \t\tstream2.skip(2).forEach(s->System.out.println(s));\n" +
                "    \t\t\n" +
                "    \t\tSystem.out.println(\"-----------------\");\n" +
                "    //\t\tdistinct、去重(需要重写 equals 和 hashCode 方法)\n" +
                "    \t\tList<Student> lists = new ArrayList<Student>();\n" +
                "    \t\tlists.add(new Student(\"A\",10));\n" +
                "    \t\tlists.add(new Student(\"A\",10));\n" +
                "    \t\tlists.add(new Student(\"A\",10));\n" +
                "    \t\t\n" +
                "    \t\tStream<Student> stream3 = lists.stream();\n" +
                "    \t\tstream3.distinct().forEach(s->System.out.println(s));\n" +
                "    \t\t\n" +
                "    //\t\tsorted() 排序 要求 类型要实现内部比较器接口。或者指定外部比较器对象。\n" +
                "    //\t\tSystem.out.println(\"-----------------\");\n" +
                "    \t\tList<Student> list3 = new ArrayList<Student>();\n" +
                "    \t\tlist3.add(new Student(\"A\",MyUtil.random(0, 100)));\n" +
                "    \t\tlist3.add(new Student(\"A\",MyUtil.random(0, 100)));\n" +
                "    \t\tlist3.add(new Student(\"A\",MyUtil.random(0, 100)));\n" +
                "    \t\t\n" +
                "    \t\tStream<Student> stream4 = list3.stream();\n" +
                "    \t\tstream4.sorted().forEach(s->System.out.println(s));\n" +
                "    \t\t\n" +
                "    \t\t\n" +
                "    //\t\tmap  对容器中的所有的元素进行统一的处理，并返回处理的结果。\n" +
                "    //\t\t将字符串集，转换为 学生对象集\n" +
                "    \t\tList<String> list2 = Arrays.asList(\"tom:10\",\"tom1:10\",\"tom2:10\",\"tom3:10\",\"tom4:10\");\n" +
                "    \t\t\n" +
                "    \t\tlist2.stream().map(s->{\n" +
                "    \t\t\tString[] split = s.split(\":\");\n" +
                "    \t\t\tString name = split[0];\n" +
                "    //\t\t\tInteger.parseInt(split[1])\n" +
                "    \t\t\tint age = Integer.valueOf(split[1]);\n" +
                "    \t\t\treturn new Student(name,age);\n" +
                "    \t\t}).forEach(s->System.out.println(s));\n" +
                "    \t\t\n" +
                "    //\t\tparallel  将串行流，转换为并行流。\n" +
                "    //\t\tlist2.parallelStream().limit(maxSize)\n" +
                "    \t\t\n" +
                "    \t}\n" +
                "    \n" +
                "    }\n" +
                "    \n" +
                "    class Student implements Comparable<Student>{\n" +
                "    \tprivate String name;\n" +
                "    \tprivate int age;\n" +
                "    \tpublic Student(String name, int age) {\n" +
                "    \t\tsuper();\n" +
                "    \t\tthis.name = name;\n" +
                "    \t\tthis.age = age;\n" +
                "    \t}\n" +
                "    \tpublic String getName() {\n" +
                "    \t\treturn name;\n" +
                "    \t}\n" +
                "    \tpublic void setName(String name) {\n" +
                "    \t\tthis.name = name;\n" +
                "    \t}\n" +
                "    \tpublic int getAge() {\n" +
                "    \t\treturn age;\n" +
                "    \t}\n" +
                "    \tpublic void setAge(int age) {\n" +
                "    \t\tthis.age = age;\n" +
                "    \t}\n" +
                "    \t@Override\n" +
                "    \tpublic String toString() {\n" +
                "    \t\treturn \"Student [name=\" + name + \", age=\" + age + \"]\";\n" +
                "    \t}\n" +
                "    \t@Override\n" +
                "    \tpublic int hashCode() {\n" +
                "    \t\tfinal int prime = 31;\n" +
                "    \t\tint result = 1;\n" +
                "    \t\tresult = prime * result + age;\n" +
                "    \t\tresult = prime * result + ((name == null) ? 0 : name.hashCode());\n" +
                "    \t\treturn result;\n" +
                "    \t}\n" +
                "    \t@Override\n" +
                "    \tpublic boolean equals(Object obj) {\n" +
                "    \t\tif (this == obj)\n" +
                "    \t\t\treturn true;\n" +
                "    \t\tif (obj == null)\n" +
                "    \t\t\treturn false;\n" +
                "    \t\tif (getClass() != obj.getClass())\n" +
                "    \t\t\treturn false;\n" +
                "    \t\tStudent other = (Student) obj;\n" +
                "    \t\tif (age != other.age)\n" +
                "    \t\t\treturn false;\n" +
                "    \t\tif (name == null) {\n" +
                "    \t\t\tif (other.name != null)\n" +
                "    \t\t\t\treturn false;\n" +
                "    \t\t} else if (!name.equals(other.name))\n" +
                "    \t\t\treturn false;\n" +
                "    \t\treturn true;\n" +
                "    \t}\n" +
                "    \t@Override\n" +
                "    \tpublic int compareTo(Student o) {\n" +
                "    \t\treturn this.age - o.age;\n" +
                "    \t}\n" +
                "    }\n" +
                "    ```\n" +
                "\n" +
                "  - \n" +
                "\n" +
                "- Stream 的终止操作\n" +
                "\n" +
                "  - forEach、min、max、count、anyMatch、allMatch、noneMatch、findFirst、findAny、toArray\n" +
                "\n" +
                "  - reduce 统计工资\n" +
                "\n" +
                "  - collect  收集人名\n" +
                "\n" +
                "  - ```java\n" +
                "    package com.qf.stream;\n" +
                "    \n" +
                "    import java.util.ArrayList;\n" +
                "    import java.util.List;\n" +
                "    import java.util.Optional;\n" +
                "    import java.util.stream.Collector;\n" +
                "    import java.util.stream.Collectors;\n" +
                "    \n" +
                "    /**\n" +
                "     * 流的终结操作:\n" +
                "     * \t1: 流的终结操作之后，流就不能再使用了。\n" +
                "     * \t2：分类\n" +
                "     * \t\t短路：找到满足要求的元素就立即结束。\n" +
                "     * \t\t非短路：要遍历所有的容器中的元素。\n" +
                "     * \n" +
                "     *  - forEach、遍历的\n" +
                "     *  min：求容器中的最小值。\n" +
                "     *  max：最大值\n" +
                "     *  count：统计个数。\n" +
                "     *  anyMatch：短路，是否有匹配的内容。\n" +
                "     *  allMatch：短路，是有所有的都匹配。\n" +
                "     *  noneMatch：是否都不匹配\n" +
                "     *  findFirst：获取到第一个元素。\n" +
                "     *  findAny：获取到任意一个元素。\n" +
                "     *  toArray：将容器中的数据保存到一个数组中。\n" +
                "     *  reduce 统计。。统计工资\n" +
                "     *  collect 收集   收集人名\n" +
                "     */\n" +
                "    public class StreamEndTest {\n" +
                "    \n" +
                "    \t@SuppressWarnings(\"unchecked\")\n" +
                "    \tpublic static void main(String[] args) {\n" +
                "    \t\tList<Student> stus = new ArrayList<Student>();\n" +
                "    \t\tstus.add(new Student(\"刘备\",30));\n" +
                "    \t\tstus.add(new Student(\"曹操\",32));\n" +
                "    \t\tstus.add(new Student(\"孙权\",25));\n" +
                "    \t\tstus.add(new Student(\"大乔\",22));\n" +
                "    \t\tstus.add(new Student(\"小乔\",16));\n" +
                "    \t\tstus.add(new Student(\"周瑜\",26));\n" +
                "    \t\tstus.add(new Student(\"张飞\",29));\n" +
                "    //\t\t判空 利器。保证了得到的结果一定不是 null。\n" +
                "    //\t\tmin：求容器中的最小值。\n" +
                "    \t\tOptional<Student> result = stus.stream().min((s1,s2)->s1.getAge()-s2.getAge());\n" +
                "    //\t\t获取里面的内容\n" +
                "    \t\tSystem.out.println(result.get());\n" +
                "    \t\t\n" +
                "    //\t\t *  max：最大值\n" +
                "    //\t\t *  count：统计个数。\n" +
                "    \t\tlong count = stus.stream().count();\n" +
                "    \t\tSystem.out.println(count);\n" +
                "    \t\t\n" +
                "    //\t\t *  anyMatch：短路，是否有匹配的内容。\n" +
                "    \t\tboolean match = stus.stream().anyMatch(s->s.getAge()==16);\n" +
                "    \t\tSystem.out.println(match);\n" +
                "    //\t\t *  allMatch：短路，是有所有的都匹配。\n" +
                "    \t\tmatch = stus.stream().allMatch(s->s.getAge()>=16);\n" +
                "    \t\tSystem.out.println(match);\n" +
                "    //\t\t *  noneMatch：是否都不匹配\n" +
                "    \t\t\n" +
                "    //\t\t *  findFirst：获取到第一个元素。\n" +
                "    \t\tOptional<Student> first = stus.stream().findFirst();\n" +
                "    \t\tSystem.out.println(first.get());\n" +
                "    //\t\t *  findAny：获取到任意一个元素。\n" +
                "    \t\tOptional<Student> any = stus.stream().findAny();\n" +
                "    \t\tSystem.out.println(any.get());\n" +
                "    //\t\t *  toArray：将容器中的数据保存到一个数组中。\n" +
                "    \t\tObject[] array = stus.stream().toArray();\n" +
                "    \t\t\n" +
                "    //\t\t *  reduce 统计。。统计年龄总和\n" +
                "    \t\tOptional<Integer> reduce = stus.stream().map(s->s.getAge()).reduce((age1,age2)->age1+age2);\n" +
                "    \t\tSystem.out.println(reduce.get());\n" +
                "    \t\t//*  collect 收集   收集人名 -->list\n" +
                "    \t\tList<String> names = stus.stream().map(s->s.getName()).collect(Collectors.toList());\n" +
                "    \t\t\n" +
                "    \t\tSystem.out.println(names);\t\n" +
                "    \t}\n" +
                "    }\n" +
                "    ```\n" +
                "\n" +
                "  - \n" +
                "\n" +
                "### 5. 新的时间API\n" +
                "\n" +
                "- 之前的时间api存在的问题：\n" +
                "\n" +
                "  - 线程非安全\n" +
                "    - 多个线程同时将一个日期字符串格式化成一个日期对象存在线程安全问题。\n" +
                "  - 时间可变\n" +
                "    - 可以对日期对象修改它的属性值。\n" +
                "\n" +
                "- java.time.LocalDateTime\n" +
                "\n" +
                "  - 本地日期时间，不可变的\n" +
                "  - 创建对象：\n" +
                "    - LocalDateTime.now()\n" +
                "    - LocalDateTime.of(......)\n" +
                "  - 实例方法：\n" +
                "    - minus...\n" +
                "    - plus....\n" +
                "    - get.....\n" +
                "    - with....\n" +
                "\n" +
                "- Instant：时间戳 \n" +
                "\n" +
                "  - Instant类在Java日期与时间功能中，表示了时间线上一个确切的点，定义为距离初始时间的时间差（初始时间为GMT 1970年1月1日00:00）\n" +
                "  - 创建对象：\n" +
                "    - Instant.now()   格林尼治时间\n" +
                "    - Instant.ofEpochMilli(milliSeconds)\n" +
                "  - toEpochMilli()\n" +
                "  - minusMillis\n" +
                "  - plusMillis\n" +
                "\n" +
                "- ZoneId 时区\n" +
                "\n" +
                "  - ZoneId.getAvailableZoneIds()\n" +
                "  - ZoneId.systemDefault()\n" +
                "\n" +
                "- Date--Instant--LocalTime （将原来的Date转换为LocalTime）\n" +
                "\n" +
                "  ```java\n" +
                "  Date date = new Date();\n" +
                "  Instant instant = date.toInstant();\n" +
                "  LocalDateTime time = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();\n" +
                "  ```\n" +
                "\n" +
                "- LocalTime--Instant--Date\n" +
                "\n" +
                "  ```java\n" +
                "  LocalDateTime time = LocalDateTime.now();\n" +
                "  Instant instant = time.atZone(ZoneId.systemDefault()).toInstant();\n" +
                "  Date date = Date.from(instant);\n" +
                "  ```\n" +
                "\n" +
                "- DateTimeFormatter\n" +
                "\n" +
                "  - 日期和字符串之间的相互转换\n" +
                "\n" +
                "  ```java\n" +
                "  LocalDateTime time = LocalDateTime.now();\n" +
                "  \n" +
                "  DateTimeFormatter dtf = DateTimeFormatter.ofPattern(\"yyyy-MM-dd HH:mm:ss\");\n" +
                "  String result = dtf.format(time);\n" +
                "  \t\t\n" +
                "  String str = \"2022-04-16 18:31:43\";\n" +
                "  LocalDateTime time2 = LocalDateTime.parse(str,dtf);\n" +
                "  ```\n" +
                "\n" +
                "  \n" +
                "\n" +
                "`Stream` 是 Java 8 引入的一个强大功能，它提供了一种高级迭代器，允许你以声明式方式处理数据集合。`Stream` 流可以看作是一个数据管道，通过它可以对数据集合进行一系列的操作，如过滤、排序、转换、聚合等。\n" +
                "\n" +
                "### Stream 的主要特性\n" +
                "\n" +
                "1. **管道式操作**：Stream 的操作可以链式调用，形成一个管道，每个操作的输出作为下一个操作的输入。\n" +
                "2. **惰性求值**：Stream 的操作不会立即执行，直到调用终止操作（如 `collect`、`forEach`、`count` 等）时才会开始执行。\n" +
                "3. **不可变性**：Stream 的操作不会修改原始数据源，而是返回一个新的 Stream 或结果。\n" +
                "4. **支持并行操作**：Stream 支持并行操作，可以通过 `parallelStream` 方法将 Stream 转换为并行 Stream，从而利用多核处理器提高性能。\n" +
                "\n" +
                "### Stream 的创建\n" +
                "\n" +
                "Stream 可以通过多种方式创建，常见的有：\n" +
                "\n" +
                "- 从集合创建\n" +
                "- 从数组创建\n" +
                "- 从文件创建\n" +
                "- 使用 `Stream` 的静态方法创建\n" +
                "\n" +
                "#### 示例：创建 Stream\n" +
                "\n" +
                "```java\n" +
                "import java.util.Arrays;\n" +
                "import java.util.List;\n" +
                "import java.util.stream.Stream;\n" +
                "\n" +
                "public class StreamCreation {\n" +
                "    public static void main(String[] args) {\n" +
                "        // 从集合创建 Stream\n" +
                "        List<String> list = Arrays.asList(\"apple\", \"banana\", \"cherry\");\n" +
                "        Stream<String> streamFromList = list.stream();\n" +
                "\n" +
                "        // 从数组创建 Stream\n" +
                "        String[] array = {\"apple\", \"banana\", \"cherry\"};\n" +
                "        Stream<String> streamFromArray = Arrays.stream(array);\n" +
                "\n" +
                "        // 使用 Stream 的静态方法创建 Stream\n" +
                "        Stream<String> streamFromStatic = Stream.of(\"apple\", \"banana\", \"cherry\");\n" +
                "\n" +
                "        // 从文件创建 Stream（需要引入 java.nio.file 包）\n" +
                "        // Stream<String> streamFromFile = Files.lines(Paths.get(\"file.txt\"));\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### Stream 的操作\n" +
                "\n" +
                "Stream 的操作可以分为中间操作和终止操作：\n" +
                "\n" +
                "- **中间操作**：返回一个新的 Stream，可以链式调用。常见的中间操作有：\n" +
                "  - `filter`：过滤 Stream 中的元素\n" +
                "  - `map`：对 Stream 中的元素进行映射\n" +
                "  - `sorted`：对 Stream 中的元素进行排序\n" +
                "  - `distinct`：去除 Stream 中的重复元素\n" +
                "- **终止操作**：返回一个结果或副作用，结束 Stream 的处理。常见的终止操作有：\n" +
                "  - `forEach`：对 Stream 中的每个元素执行操作\n" +
                "  - `collect`：将 Stream 中的元素收集到集合中\n" +
                "  - `count`：统计 Stream 中元素的数量\n" +
                "  - `reduce`：对 Stream 中的元素进行归并操作\n" +
                "\n" +
                "#### 示例：Stream 操作\n" +
                "\n" +
                "```java\n" +
                "import java.util.Arrays;\n" +
                "import java.util.List;\n" +
                "import java.util.stream.Collectors;\n" +
                "\n" +
                "public class StreamOperations {\n" +
                "    public static void main(String[] args) {\n" +
                "        List<String> fruits = Arrays.asList(\"apple\", \"banana\", \"cherry\", \"apple\");\n" +
                "\n" +
                "        // 中间操作：过滤、映射、去重、排序\n" +
                "        List<String> result = fruits.stream()\n" +
                "                .filter(fruit -> fruit.startsWith(\"a\")) // 过滤以 \"a\" 开头的水果\n" +
                "                .map(String::toUpperCase) // 将水果名称转为大写\n" +
                "                .distinct() // 去重\n" +
                "                .sorted() // 排序\n" +
                "                .collect(Collectors.toList()); // 收集到列表\n" +
                "\n" +
                "        // 终止操作：遍历、计数\n" +
                "        result.forEach(System.out::println); // 遍历并打印\n" +
                "        long count = fruits.stream().filter(fruit -> fruit.startsWith(\"a\")).count(); // 统计以 \"a\" 开头的水果数量\n" +
                "        System.out.println(\"Count: \" + count);\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### Stream 的并行操作\n" +
                "\n" +
                "Stream 支持并行操作，可以通过 `parallelStream` 方法将 Stream 转换为并行 Stream，从而利用多核处理器提高性能。\n" +
                "\n" +
                "#### 示例：并行 Stream 操作\n" +
                "\n" +
                "```java\n" +
                "import java.util.Arrays;\n" +
                "import java.util.List;\n" +
                "\n" +
                "public class ParallelStreamExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        List<String> fruits = Arrays.asList(\"apple\", \"banana\", \"cherry\", \"apple\");\n" +
                "\n" +
                "        // 并行 Stream 操作\n" +
                "        fruits.parallelStream()\n" +
                "                .filter(fruit -> fruit.startsWith(\"a\"))\n" +
                "                .map(String::toUpperCase)\n" +
                "                .distinct()\n" +
                "                .sorted()\n" +
                "                .forEach(System.out::println);\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### Stream 的常见用法\n" +
                "\n" +
                "#### 1. 过滤\n" +
                "\n" +
                "```java\n" +
                "List<String> fruits = Arrays.asList(\"apple\", \"banana\", \"cherry\", \"apple\");\n" +
                "List<String> filteredFruits = fruits.stream()\n" +
                "        .filter(fruit -> fruit.startsWith(\"a\"))\n" +
                "        .collect(Collectors.toList());\n" +
                "System.out.println(filteredFruits); // 输出：[apple, apple]\n" +
                "```\n" +
                "\n" +
                "#### 2. 映射\n" +
                "\n" +
                "```java\n" +
                "List<String> fruits = Arrays.asList(\"apple\", \"banana\", \"cherry\");\n" +
                "List<String> upperCaseFruits = fruits.stream()\n" +
                "        .map(String::toUpperCase)\n" +
                "        .collect(Collectors.toList());\n" +
                "System.out.println(upperCaseFruits); // 输出：[APPLE, BANANA, CHERRY]\n" +
                "```\n" +
                "\n" +
                "#### 3. 去重\n" +
                "\n" +
                "```java\n" +
                "List<String> fruits = Arrays.asList(\"apple\", \"banana\", \"cherry\", \"apple\");\n" +
                "List<String> distinctFruits = fruits.stream()\n" +
                "        .distinct()\n" +
                "        .collect(Collectors.toList());\n" +
                "System.out.println(distinctFruits); // 输出：[apple, banana, cherry]\n" +
                "```\n" +
                "\n" +
                "#### 4. 排序\n" +
                "\n" +
                "```java\n" +
                "List<String> fruits = Arrays.asList(\"apple\", \"banana\", \"cherry\");\n" +
                "List<String> sortedFruits = fruits.stream()\n" +
                "        .sorted()\n" +
                "        .collect(Collectors.toList());\n" +
                "System.out.println(sortedFruits); // 输出：[apple, banana, cherry]\n" +
                "```\n" +
                "\n" +
                "#### 5. 收集\n" +
                "\n" +
                "```java\n" +
                "List<String> fruits = Arrays.asList(\"apple\", \"banana\", \"cherry\");\n" +
                "List<String> collectedFruits = fruits.stream()\n" +
                "        .collect(Collectors.toList());\n" +
                "System.out.println(collectedFruits); // 输出：[apple, banana, cherry]\n" +
                "```\n" +
                "\n" +
                "#### 6. 计数\n" +
                "\n" +
                "```java\n" +
                "List<String> fruits = Arrays.asList(\"apple\", \"banana\", \"cherry\");\n" +
                "long count = fruits.stream()\n" +
                "        .filter(fruit -> fruit.startsWith(\"a\"))\n" +
                "        .count();\n" +
                "System.out.println(count); // 输出：2\n" +
                "```\n" +
                "\n" +
                "#### 7. 归并\n" +
                "\n" +
                "```java\n" +
                "List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);\n" +
                "int sum = numbers.stream()\n" +
                "        .reduce(0, (a, b) -> a + b);\n" +
                "System.out.println(sum); // 输出：15\n" +
                "```\n" +
                "\n" +
                "### 总结\n" +
                "\n" +
                "`Stream` 是 Java 8 引入的一个强大功能，它提供了一种高级迭代器，允许你以声明式方式处理数据集合。通过合理使用 Stream 的中间操作和终止操作，可以编写出更简洁、更高效、更易读的代码。Stream 支持并行操作，可以通过 `parallelStream` 方法将 Stream 转换为并行 Stream，从而利用多核处理器提高性能。\n" +
                "\n" +
                "# I/O流\n" +
                "\n" +
                "Java中的I/O（输入/输出）流是用于处理数据输入和输出的机制。I/O流可以处理各种类型的数据，包括文件、网络传输、内存等。Java的I/O流主要分为两大类：字节流和字符流。\n" +
                "\n" +
                "i/o的应用：1.读取配置文件，2.文件的上传，下载。3.网络传输 发消息，字符，数字，图片通通转成字节流，根据我们网络传输，字节流转成字符流进行读取\n" +
                "\n" +
                "### 1. 字节流\n" +
                "\n" +
                "字节流用于处理二进制数据，如图片、音频文件等。字节流的基类是`InputStream`和`OutputStream`。\n" +
                "\n" +
                "#### 1.1 输入流（InputStream）\n" +
                "\n" +
                "输入流用于从数据源读取数据。常见的输入流类包括：\n" +
                "\n" +
                "- `FileInputStream`：从文件中读取数据。\n" +
                "- `ByteArrayInputStream`：从字节数组中读取数据。\n" +
                "- `BufferedInputStream`：提供缓冲功能，提高读取效率。//缓存区，如果我们读取大文件，\n" +
                "- `DataInputStream`：用于读取基本数据类型（如int、double等）的数据。\n" +
                "\n" +
                "**示例代码：从文件中读取数据**\n" +
                "\n" +
                "```java\n" +
                "import java.io.FileInputStream;\n" +
                "import java.io.IOException;\n" +
                "\n" +
                "public class FileInputStreamExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        try (FileInputStream fis = new FileInputStream(\"example.txt\")) {\n" +
                "            int content;\n" +
                "            while ((content = fis.read()) != -1) {\n" +
                "                System.out.print((char) content);\n" +
                "            }\n" +
                "        } catch (IOException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "#### 1.2 输出流（OutputStream）\n" +
                "\n" +
                "输出流用于向目标写入数据。常见的输出流类包括：\n" +
                "\n" +
                "- `FileOutputStream`：向文件写入数据。\n" +
                "- `ByteArrayOutputStream`：向字节数组写入数据。\n" +
                "- BufferedOutputStream`：提供缓冲功能，提高写入效率。\n" +
                "- `DataOutputStream`：用于写入基本数据类型的数据。\n" +
                "\n" +
                "**示例代码：向文件写入数据**\n" +
                "\n" +
                "```java\n" +
                "import java.io.FileOutputStream;\n" +
                "import java.io.IOException;\n" +
                "\n" +
                "public class FileOutputStreamExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        try (FileOutputStream fos = new FileOutputStream(\"example.txt\")) {\n" +
                "            String content = \"Hello, World!\";\n" +
                "            fos.write(content.getBytes());\n" +
                "        } catch (IOException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 2. 字符流\n" +
                "\n" +
                "字符流用于处理文本数据，如文本文件等。字符流的基类是`Reader`和`Writer`。\n" +
                "\n" +
                "#### 2.1 输入流（Reader）\n" +
                "\n" +
                "输入流用于从数据源读取文本数据。常见的输入流类包括：\n" +
                "\n" +
                "- `FileReader`：从文件中读取文本数据。\n" +
                "- `BufferedReader`：提供缓冲功能，提高读取效率。\n" +
                "- `InputStreamReader`：将字节流转换为字符流。\n" +
                "\n" +
                "**示例代码：从文件中读取文本数据**\n" +
                "\n" +
                "```java\n" +
                "import java.io.BufferedReader;\n" +
                "import java.io.FileReader;\n" +
                "import java.io.IOException;\n" +
                "\n" +
                "public class FileReaderExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        try (BufferedReader br = new BufferedReader(new FileReader(\"example.txt\"))) {\n" +
                "            String line;\n" +
                "            while ((line = br.readLine()) != null) {\n" +
                "                System.out.println(line);\n" +
                "            }\n" +
                "        } catch (IOException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "#### 2.2 输出流（Writer）\n" +
                "\n" +
                "输出流用于向目标写入文本数据。常见的输出流类包括：\n" +
                "\n" +
                "- `FileWriter`：向文件写入文本数据。\n" +
                "- `BufferedWriter`：提供缓冲功能，提高写入效率。\n" +
                "- `OutputStreamWriter`：将字节流转换为字符流。\n" +
                "\n" +
                "**示例代码：向文件写入文本数据**\n" +
                "\n" +
                "```java\n" +
                "import java.io.BufferedWriter;\n" +
                "import java.io.FileWriter;\n" +
                "import java.io.IOException;\n" +
                "\n" +
                "public class FileWriterExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        try (BufferedWriter bw = new BufferedWriter(new FileWriter(\"example.txt\"))) {\n" +
                "            String content = \"Hello, World!\";\n" +
                "            bw.write(content);\n" +
                "        } catch (IOException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 3. 装饰器模式\n" +
                "\n" +
                "Java I/O流采用了装饰器模式，允许通过组合多个流来增强功能。例如，`BufferedInputStream`和`BufferedOutputStream`可以为输入和输出流提供缓冲功能，从而提高读写效率。\n" +
                "\n" +
                "**示例代码：使用缓冲流**\n" +
                "\n" +
                "```java\n" +
                "import java.io.*;\n" +
                "\n" +
                "public class BufferedStreamExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        // 使用缓冲输入流\n" +
                "        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(\"example.txt\"))) {\n" +
                "            int content;\n" +
                "            while ((content = bis.read()) != -1) {\n" +
                "                System.out.print((char) content);\n" +
                "            }\n" +
                "        } catch (IOException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "\n" +
                "        // 使用缓冲输出流\n" +
                "        try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(\"example.txt\"))) {\n" +
                "            String content = \"Hello, World!\";\n" +
                "            bos.write(content.getBytes());\n" +
                "        } catch (IOException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 4. 序列化和反序列化\n" +
                "\n" +
                "序列化是将对象转换为字节序列的过程，反序列化则是将字节序列还原为对象的过程。Java提供了`ObjectOutputStream`和`ObjectInputStream`来实现序列化和反序列化。\n" +
                "\n" +
                "作用：持久化数据, 将数据存在磁盘或者是内存当中，网络传输\n" +
                "\n" +
                "**示例代码：序列化和反序列化**\n" +
                "\n" +
                "```java\n" +
                "import java.io.*;\n" +
                "\n" +
                "public class SerializationExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        // 序列化\n" +
                "        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(\"object.dat\"))) {\n" +
                "            Person person = new Person(\"Alice\", 30);\n" +
                "            oos.writeObject(person);\n" +
                "        } catch (IOException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "\n" +
                "        // 反序列化\n" +
                "        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(\"object.dat\"))) {\n" +
                "            Person person = (Person) ois.readObject();\n" +
                "            System.out.println(person.getName() + \", \" + person.getAge());\n" +
                "        } catch (IOException | ClassNotFoundException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "\n" +
                "class Person implements Serializable {\n" +
                "    private String name;\n" +
                "    private int age;\n" +
                "\n" +
                "    public Person(String name, int age) {\n" +
                "        this.name = name;\n" +
                "        this.age = age;\n" +
                "    }\n" +
                "\n" +
                "    public String getName() {\n" +
                "        return name;\n" +
                "    }\n" +
                "\n" +
                "    public int getAge() {\n" +
                "        return age;\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 5. NIO（New I/O）\n" +
                "\n" +
                "Java NIO是Java I/O的扩展，提供了非阻塞I/O操作、通道（Channel）和缓冲区（Buffer）等新特性。NIO的性能通常优于传统的I/O，适用于高性能的网络和文件I/O操作。\n" +
                "\n" +
                "**示例代码：使用NIO读取文件**\n" +
                "\n" +
                "```java\n" +
                "import java.nio.ByteBuffer;\n" +
                "import java.nio.channels.FileChannel;\n" +
                "import java.nio.file.Paths;\n" +
                "import java.nio.file.StandardOpenOption;\n" +
                "\n" +
                "public class NIOExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        try (FileChannel fileChannel = FileChannel.open(Paths.get(\"example.txt\"), StandardOpenOption.READ)) {\n" +
                "            ByteBuffer buffer = ByteBuffer.allocate(1024);\n" +
                "            while (fileChannel.read(buffer) != -1) {\n" +
                "                buffer.flip();\n" +
                "                while (buffer.hasRemaining()) {\n" +
                "                    System.out.print((char) buffer.get());\n" +
                "                }\n" +
                "                buffer.clear();\n" +
                "            }\n" +
                "        } catch (IOException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 总结\n" +
                "\n" +
                "Java I/O流提供了丰富的功能，用于处理各种数据输入和输出操作。字节流和字符流分别用于处理二进制数据和文本数据，装饰器模式允许通过组合多个流来增强功能。序列化和反序列化用于对象的持久化，NIO提供了高性能的I/O操作。通过合理使用这些I/O流，可以满足各种数据处理需求。\n" +
                "\n" +
                "在Java中，文件操作是通过`java.io`包和`java.nio`包中的类和接口来实现的。这些类和接口提供了丰富的功能，用于读取、写入、创建、删除文件以及管理文件的元数据等。以下是一些常见的文件操作示例：\n" +
                "\n" +
                "### 1. 创建文件\n" +
                "\n" +
                "可以使用`File`类和`FileWriter`类来创建文件。\n" +
                "\n" +
                "**示例代码：创建文件**\n" +
                "\n" +
                "```java\n" +
                "import java.io.File;\n" +
                "import java.io.FileWriter;\n" +
                "import java.io.IOException;\n" +
                "\n" +
                "public class CreateFileExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        String filePath = \"example.txt\";\n" +
                "\n" +
                "        try (FileWriter writer = new FileWriter(filePath)) {\n" +
                "            writer.write(\"Hello, World!\");\n" +
                "            System.out.println(\"文件创建成功，并写入内容\");\n" +
                "        } catch (IOException e) {\n" +
                "            System.out.println(\"文件创建失败\");\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 2. 读取文件\n" +
                "\n" +
                "可以使用`FileReader`和`BufferedReader`来读取文件内容。\n" +
                "\n" +
                "**示例代码：读取文件**\n" +
                "\n" +
                "```java\n" +
                "import java.io.BufferedReader;\n" +
                "import java.io.FileReader;\n" +
                "import java.io.IOException;\n" +
                "\n" +
                "public class ReadFileExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        String filePath = \"example.txt\";\n" +
                "\n" +
                "        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {\n" +
                "            String line;\n" +
                "            while ((line = reader.readLine()) != null) {\n" +
                "                System.out.println(line);\n" +
                "            }\n" +
                "        } catch (IOException e) {\n" +
                "            System.out.println(\"文件读取失败\");\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 3. 写入文件\n" +
                "\n" +
                "可以使用`FileWriter`和`BufferedWriter`来写入文件内容。\n" +
                "\n" +
                "**示例代码：写入文件**\n" +
                "\n" +
                "```java\n" +
                "import java.io.BufferedWriter;\n" +
                "import java.io.FileWriter;\n" +
                "import java.io.IOException;\n" +
                "\n" +
                "public class WriteFileExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        String filePath = \"example.txt\";\n" +
                "\n" +
                "        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {\n" +
                "            writer.write(\"\\nNew line added\");\n" +
                "            System.out.println(\"内容写入成功\");\n" +
                "        } catch (IOException e) {\n" +
                "            System.out.println(\"文件写入失败\");\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 4. 删除文件\n" +
                "\n" +
                "可以使用`File`类的`delete`方法来删除文件。\n" +
                "\n" +
                "**示例代码：删除文件**\n" +
                "\n" +
                "```java\n" +
                "import java.io.File;\n" +
                "\n" +
                "public class DeleteFileExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        String filePath = \"example.txt\";\n" +
                "\n" +
                "        File file = new File(filePath);\n" +
                "        if (file.delete()) {\n" +
                "            System.out.println(\"文件删除成功\");\n" +
                "        } else {\n" +
                "            System.out.println(\"文件删除失败\");\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 5. 检查文件是否存在\n" +
                "\n" +
                "可以使用`File`类的`exists`方法来检查文件是否存在。\n" +
                "\n" +
                "**示例代码：检查文件是否存在**\n" +
                "\n" +
                "```java\n" +
                "import java.io.File;\n" +
                "\n" +
                "public class CheckFileExistsExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        String filePath = \"example.txt\";\n" +
                "\n" +
                "        File file = new File(filePath);\n" +
                "        if (file.exists()) {\n" +
                "            System.out.println(\"文件存在\");\n" +
                "        } else {\n" +
                "            System.out.println(\"文件不存在\");\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 6. 获取文件信息\n" +
                "\n" +
                "可以使用`File`类的方法来获取文件的元数据，如文件大小、最后修改时间等。\n" +
                "\n" +
                "**示例代码：获取文件信息**\n" +
                "\n" +
                "```java\n" +
                "import java.io.File;\n" +
                "\n" +
                "public class GetFileInfoExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        String filePath = \"example.txt\";\n" +
                "\n" +
                "        File file = new File(filePath);\n" +
                "        if (file.exists()) {\n" +
                "            System.out.println(\"文件名: \" + file.getName());\n" +
                "            System.out.println(\"文件路径: \" + file.getAbsolutePath());\n" +
                "            System.out.println(\"文件大小: \" + file.length() + \" 字节\");\n" +
                "            System.out.println(\"最后修改时间: \" + file.lastModified());\n" +
                "        } else {\n" +
                "            System.out.println(\"文件不存在\");\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 7. 创建目录\n" +
                "\n" +
                "可以使用`File`类的`mkdir`和`mkdirs`方法来创建目录。\n" +
                "\n" +
                "**示例代码：创建目录**\n" +
                "\n" +
                "```java\n" +
                "import java.io.File;\n" +
                "\n" +
                "public class CreateDirectoryExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        String dirPath = \"exampleDir\";\n" +
                "\n" +
                "        File dir = new File(dirPath);\n" +
                "        if (dir.mkdir()) {\n" +
                "            System.out.println(\"目录创建成功\");\n" +
                "        } else {\n" +
                "            System.out.println(\"目录创建失败\");\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 8. 遍历目录\n" +
                "\n" +
                "可以使用`File`类的`listFiles`方法来遍历目录中的文件和子目录。\n" +
                "\n" +
                "**示例代码：遍历目录**\n" +
                "\n" +
                "```java\n" +
                "import java.io.File;\n" +
                "\n" +
                "public class ListDirectoryExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        String dirPath = \"exampleDir\";\n" +
                "\n" +
                "        File dir = new File(dirPath);\n" +
                "        if (dir.exists() && dir.isDirectory()) {\n" +
                "            File[] files = dir.listFiles();\n" +
                "            if (files != null) {\n" +
                "                for (File file : files) {\n" +
                "                    System.out.println(file.getName());\n" +
                "                }\n" +
                "            }\n" +
                "        } else {\n" +
                "            System.out.println(\"目录不存在\");\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 9. 使用NIO操作文件\n" +
                "\n" +
                "Java NIO提供了更高效的文件操作方式，如`Files`类和`Path`接口。\n" +
                "\n" +
                "**示例代码：使用NIO创建文件**\n" +
                "\n" +
                "```java\n" +
                "import java.nio.file.Files;\n" +
                "import java.nio.file.Path;\n" +
                "import java.nio.file.Paths;\n" +
                "import java.nio.file.StandardOpenOption;\n" +
                "import java.io.IOException;\n" +
                "\n" +
                "public class NIOCreateFileExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        String filePath = \"example.txt\";\n" +
                "\n" +
                "        Path path = Paths.get(filePath);\n" +
                "        try {\n" +
                "            Files.createFile(path);\n" +
                "            System.out.println(\"文件创建成功\");\n" +
                "        } catch (IOException e) {\n" +
                "            System.out.println(\"文件创建失败\");\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "**示例代码：使用NIO读取文件**\n" +
                "\n" +
                "```java\n" +
                "import java.nio.file.Files;\n" +
                "import java.nio.file.Path;\n" +
                "import java.nio.file.Paths;\n" +
                "import java.io.IOException;\n" +
                "\n" +
                "public class NIOWriteFileExample {\n" +
                "    public static void main(String[] args) {\n" +
                "        String filePath = \"example.txt\";\n" +
                "\n" +
                "        Path path = Paths.get(filePath);\n" +
                "        try {\n" +
                "            String content = new String(Files.readAllBytes(path));\n" +
                "            System.out.println(content);\n" +
                "        } catch (IOException e) {\n" +
                "            System.out.println(\"文件读取失败\");\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 总结\n" +
                "\n" +
                "Java提供了丰富的文件操作功能，通过`java.io`和`java.nio`包中的类和接口，可以实现文件的创建、读取、写入、删除、检查等操作。合理使用这些功能，可以满足各种文件处理需求。\n" +
                "\n" +
                "\n" +
                "\n" +
                "在网络编程中，**Socket** 是一种非常重要的通信机制，用于在不同主机之间的应用程序之间建立通信。在Java中，可以通过`java.net.Socket`和`java.net.ServerSocket`类来实现基于TCP的网络通信。\n" +
                "\n" +
                "### 1. 基本概念\n" +
                "\n" +
                "- **Socket**：用于客户端和服务器之间的通信。客户端通过`Socket`连接到服务器，服务器通过`ServerSocket`监听客户端的连接请求。\n" +
                "- **TCP（传输控制协议）**：是一种面向连接的、可靠的、基于字节流的传输层通信协议。TCP保证数据的可靠传输，适用于需要高可靠性的应用，如文件传输、网页浏览等。\n" +
                "\n" +
                "### 2. 服务器端编程\n" +
                "\n" +
                "服务器端使用`ServerSocket`来监听客户端的连接请求。当客户端发起连接请求时，服务器端通过`accept`方法接受连接，并为每个客户端创建一个`Socket`对象。\n" +
                "\n" +
                "**示例代码：服务器端**\n" +
                "\n" +
                "```java\n" +
                "import java.io.*;\n" +
                "import java.net.*;\n" +
                "\n" +
                "public class Server {\n" +
                "    public static void main(String[] args) {\n" +
                "        int port = 1234; // 服务器端口\n" +
                "        try (ServerSocket serverSocket = new ServerSocket(port)) {\n" +
                "            System.out.println(\"服务器已启动，等待客户端连接...\");\n" +
                "\n" +
                "            // 接受客户端连接\n" +
                "            Socket clientSocket = serverSocket.accept();\n" +
                "            System.out.println(\"客户端已连接\");\n" +
                "\n" +
                "            // 获取输入流和输出流\n" +
                "            try (BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));\n" +
                "                 PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {\n" +
                "\n" +
                "                // 从客户端读取数据\n" +
                "                String inputLine;\n" +
                "                while ((inputLine = in.readLine()) != null) {\n" +
                "                    System.out.println(\"客户端发送: \" + inputLine);\n" +
                "\n" +
                "                    // 向客户端发送响应\n" +
                "                    out.println(\"服务器响应: \" + inputLine);\n" +
                "                }\n" +
                "            }\n" +
                "        } catch (IOException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 3. 客户端编程\n" +
                "\n" +
                "客户端使用`Socket`连接到服务器。连接成功后，客户端可以通过`Socket`的输入流和输出流与服务器进行通信。\n" +
                "\n" +
                "**示例代码：客户端**\n" +
                "\n" +
                "```java\n" +
                "import java.io.*;\n" +
                "import java.net.*;\n" +
                "\n" +
                "public class Client {\n" +
                "    public static void main(String[] args) {\n" +
                "        String host = \"localhost\"; // 服务器地址\n" +
                "        int port = 1234; // 服务器端口\n" +
                "\n" +
                "        try (Socket socket = new Socket(host, port)) {\n" +
                "            System.out.println(\"已连接到服务器\");\n" +
                "\n" +
                "            // 获取输入流和输出流\n" +
                "            try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));\n" +
                "                 PrintWriter out = new PrintWriter(socket.getOutputStream(), true);\n" +
                "                 BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {\n" +
                "\n" +
                "                String userInput;\n" +
                "                while ((userInput = stdIn.readLine()) != null) {\n" +
                "                    out.println(userInput); // 向服务器发送数据\n" +
                "\n" +
                "                    // 从服务器读取响应\n" +
                "                    System.out.println(\"服务器响应: \" + in.readLine());\n" +
                "                }\n" +
                "            }\n" +
                "        } catch (IOException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 4. 多线程服务器\n" +
                "\n" +
                "在实际应用中，服务器通常需要同时处理多个客户端的连接。可以通过多线程来实现这一点。每个客户端连接都会启动一个新线程来处理通信。\n" +
                "\n" +
                "**示例代码：多线程服务器**\n" +
                "\n" +
                "```java\n" +
                "import java.io.*;\n" +
                "import java.net.*;\n" +
                "\n" +
                "public class MultiThreadedServer {\n" +
                "    public static void main(String[] args) {\n" +
                "        int port = 1234; // 服务器端口\n" +
                "        try (ServerSocket serverSocket = new ServerSocket(port)) {\n" +
                "            System.out.println(\"服务器已启动，等待客户端连接...\");\n" +
                "\n" +
                "            while (true) {\n" +
                "                // 接受客户端连接\n" +
                "                Socket clientSocket = serverSocket.accept();\n" +
                "                System.out.println(\"客户端已连接\");\n" +
                "\n" +
                "                // 为每个客户端启动一个新线程\n" +
                "                new Thread(new ClientHandler(clientSocket)).start();\n" +
                "            }\n" +
                "        } catch (IOException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "\n" +
                "    static class ClientHandler implements Runnable {\n" +
                "        private final Socket clientSocket;\n" +
                "\n" +
                "        public ClientHandler(Socket socket) {\n" +
                "            this.clientSocket = socket;\n" +
                "        }\n" +
                "\n" +
                "        @Override\n" +
                "        public void run() {\n" +
                "            try (BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));\n" +
                "                 PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {\n" +
                "\n" +
                "                String inputLine;\n" +
                "                while ((inputLine = in.readLine()) != null) {\n" +
                "                    System.out.println(\"客户端发送: \" + inputLine);\n" +
                "\n" +
                "                    // 向客户端发送响应\n" +
                "                    out.println(\"服务器响应: \" + inputLine);\n" +
                "                }\n" +
                "            } catch (IOException e) {\n" +
                "                e.printStackTrace();\n" +
                "            } finally {\n" +
                "                try {\n" +
                "                    clientSocket.close();\n" +
                "                } catch (IOException e) {\n" +
                "                    e.printStackTrace();\n" +
                "                }\n" +
                "            }\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 5. UDP通信\n" +
                "\n" +
                "UDP（用户数据报协议）是一种无连接的、不可靠的传输层协议。UDP不保证数据的可靠传输，但传输速度较快，适用于对实时性要求较高的应用，如视频流、在线游戏等。\n" +
                "\n" +
                "在Java中，可以通过`DatagramSocket`和`DatagramPacket`类来实现基于UDP的通信。\n" +
                "\n" +
                "**示例代码：UDP服务器**\n" +
                "\n" +
                "```java\n" +
                "import java.net.*;\n" +
                "\n" +
                "public class UDPServer {\n" +
                "    public static void main(String[] args) {\n" +
                "        int port = 1234; // 服务器端口\n" +
                "        try (DatagramSocket serverSocket = new DatagramSocket(port)) {\n" +
                "            System.out.println(\"UDP服务器已启动，等待客户端消息...\");\n" +
                "\n" +
                "            byte[] receiveData = new byte[1024];\n" +
                "            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);\n" +
                "\n" +
                "            while (true) {\n" +
                "                // 接收客户端消息\n" +
                "                serverSocket.receive(receivePacket);\n" +
                "                String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength());\n" +
                "                System.out.println(\"客户端发送: \" + sentence);\n" +
                "\n" +
                "                // 向客户端发送响应\n" +
                "                InetAddress IPAddress = receivePacket.getAddress();\n" +
                "                int portNumber = receivePacket.getPort();\n" +
                "                byte[] sendData = (\"服务器响应: \" + sentence).getBytes();\n" +
                "                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, portNumber);\n" +
                "                serverSocket.send(sendPacket);\n" +
                "            }\n" +
                "        } catch (IOException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "**示例代码：UDP客户端**\n" +
                "\n" +
                "```java\n" +
                "import java.net.*;\n" +
                "\n" +
                "public class UDPClient {\n" +
                "    public static void main(String[] args) {\n" +
                "        String host = \"localhost\"; // 服务器地址\n" +
                "        int port = 1234; // 服务器端口\n" +
                "\n" +
                "        try (DatagramSocket clientSocket = new DatagramSocket()) {\n" +
                "            InetAddress IPAddress = InetAddress.getByName(host);\n" +
                "\n" +
                "            // 向服务器发送消息\n" +
                "            byte[] sendData = \"Hello, Server!\".getBytes();\n" +
                "            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);\n" +
                "            clientSocket.send(sendPacket);\n" +
                "\n" +
                "            // 接收服务器响应\n" +
                "            byte[] receiveData = new byte[1024];\n" +
                "            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);\n" +
                "            clientSocket.receive(receivePacket);\n" +
                "            String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength());\n" +
                "            System.out.println(\"服务器响应: \" + sentence);\n" +
                "        } catch (IOException e) {\n" +
                "            e.printStackTrace();\n" +
                "        }\n" +
                "    }\n" +
                "}\n" +
                "```\n" +
                "\n" +
                "### 总结\n" +
                "\n" +
                "- **TCP通信**：通过`Socket`和`ServerSocket`实现，适用于需要高可靠性的应用。\n" +
                "- **UDP通信**：通过`DatagramSocket`和`DatagramPacket`实现，适用于对实时性要求较高的应用。\n" +
                "- **多线程**：服务器端可以通过多线程同时处理多个客户端的连接。\n" +
                "- **应用场景**：TCP适用于文件传输、网页浏览等，UDP适用于视频流、在线游戏等。\n" +
                "\n" +
                "通过这些基本的网络编程知识，你可以构建各种基于网络的应用程序。");

        System.out.println("写入成功");

    }
}